﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.OleDb;


namespace 企业人事管理系统
{
    public class OledbOP
    {
        private OledbOP()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }
        public static string defaultConnect = DataName.testConStr;

        //private static string conStr = defaultConnect;
        private static OleDbConnection connect = new OleDbConnection(defaultConnect);

        public static void initConnect(string con)
        {
            if (connect.ConnectionString.CompareTo(con) != 0)
                connect = new OleDbConnection(con);
            connect.Close();
        }

        public static OleDbDataReader getReader(string sql)
        {
            try
            {
                OleDbCommand com = new OleDbCommand(sql, connect);
                connect.Open();

                OleDbDataReader reader = com.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
                return reader;

            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static DataTable getTable(string sql)
        {
            try
            {
                OleDbDataAdapter ad = new OleDbDataAdapter(sql, connect);
                DataSet s = new DataSet();
                ad.Fill(s);
                return s.Tables[0];
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static OleDbDataAdapter getAdapter(string sql)
        {
            try
            {
                OleDbDataAdapter ad = new OleDbDataAdapter(sql, connect);
                return ad;
            }
            catch(Exception e)
            {
                throw e;
            }
        }

        public static int execute(string sql)
        {
            try
            {
                OleDbCommand com = new OleDbCommand(sql, connect);
                connect.Open();
                return com.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                try
                {
                    connect.Close();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }


    }
}
